/// <reference path="./wx.d.ts" />
export const WechatUtil={

    commonShare:null,
    configWxShareP:function(resolver:WechatShareResolver){
        wx.ready(()=>{
            let message=this.commonShare
            if(!resolver && !this.commonShare){
                alert("尚未配置微信分享信息！");
                return;
            }
            wx.checkJsApi({
                jsApiList:['onMenuShareAppMessage'],
                success:function (res) {
                    let result:any=true;
                    if(!resolver){
                        message=this.commonShare;
                    } else if(resolver.beforeShare) {
                        result=resolver.beforeShare();
                        message=resolver.message;
                    }else{
                        message=resolver.message ||this.commonShare;
                    }
                    if(result instanceof Promise){
                        result.then(()=>wx.onMenuShareAppMessage(message));
                    }else{
                        if(result) wx.onMenuShareAppMessage(message);
                    }
                }
            });
            wx.checkJsApi({
                jsApiList:['onMenuShareTimeline'],
                success:function (res) {
                    wx.onMenuShareTimeline(message);
                }
            })
        })

    },
    configWxShare:function(message:wx.SharedAppMessage){
        wx.ready(()=>{
            if(!message){
                if(!this.commonShare){
                    alert("尚未配置微信分享信息！");
                    return;
                }
                message=this.commonShare;
            }
            wx.checkJsApi({
                jsApiList:['onMenuShareAppMessage'],
                success:function (res) {
                    wx.onMenuShareAppMessage(message);
                }
            });
            wx.checkJsApi({
                jsApiList:['onMenuShareTimeline'],
                success:function (res) {
                    wx.onMenuShareTimeline(message);
                }
            })
        })

    },
    scan(){
        return new Promise((resolve,reject)=>{
            wx.scanQRCode({
                needResult : 1, // 默认为0，扫描结果由微信处理，1则直接返回扫描结果，
                scanType : [ "qrCode"], // 可以指定扫二维码还是一维码，默认二者都有
                success : function(res) {
                    let result = res.resultStr; // 当needResult 为 1 时，扫码返回的结果
                    resolve(result);
                }
            });
        })

    }
};
export interface WechatShareResolver{
    message:wx.SharedAppMessage;
    beforeShare?();
    afterShare?();
}